package dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import bean.Point;
import dao.PointDao;
import exception.DBException;

public class PointDaoImpl extends HibernateDaoSupport implements PointDao {
	private StaticData staticData;

	@Override
	public boolean update(List<Point> points) throws DBException {
		this.getHibernateTemplate().saveOrUpdateAll(points);
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Point> selectAll(int area) throws DBException {
		if (staticData == null) {
			return this.getHibernateTemplate().find("from Point");
		}
		return staticData.getAllPoints(area);
	}

	@Override
	public Point queryByModel(Point point) throws DBException {
		List<Point> allps=staticData.getAllPoints(point.getArea().getId());
		for(Point p:allps){
			if(p.equals(point)){
				return p;
			}
		}
		return null;
	}

	@Override
	public boolean update(Point point) throws DBException {
		this.getHibernateTemplate().save(point);
		return true;
	}

	@Override
	public Point query(int id) throws DBException {
		List<Point> allps=staticData.getAllPoints(-1);
		for(Point p:allps){
			if(p.getId()==id){
				return p;
			}
		}
		return null;
	}

	public StaticData getStaticData() {
		return staticData;
	}

	public void setStaticData(StaticData staticData) {
		this.staticData = staticData;
	}
}
